// plugins/sanitize.ts
export default defineNuxtPlugin(async () => {
  if (process.client) {
    // 客户端不提供任何东西，避免错误
    return {
      provide: {
        sanitizeHtml: (html: string) => html, // 客户端直接返回原始 HTML（或者空）
      },
    }
  }

  // 服务端才加载 jsdom 和 DOMPurify
  const { JSDOM } = await import('jsdom')
  const createDOMPurify = (await import('dompurify')).default

  const window = new JSDOM('').window
  const DOMPurify = createDOMPurify(window)

  return {
    provide: {
      sanitizeHtml: (html: string) => DOMPurify.sanitize(html),
    },
  }
})
